layui.extend({
    common: "modules/common",
    treeGird: "../layui/lay/modules/treeGird"
}).define(["form", "layer", "table", "common", "treeGird"], function(e) {
    "use strict";
    var c = layui.form,
        r = layui.table,
        s = layui.layer,
        u = layui.common,
        d = layui.$;
    var f, h, m, g, b = 0,
        v = 0,
        y;
    var o = {
        /**
         *
         * @param e cols table表格
         * @param o #tableList 数据要填充的表格ID名称
         * @param t function 方法 对于删除 编辑的点击事件
         * @param n 数据请求的url（e.g. 列表json） 不指定默认 A+'/index'
         * @param i 是否启用排序
         * @param c add by liangpingzheng 用于自己定义field 默认删除时取的主键ID,如果主键名字不是ID时，删除时就会提示找不了主键了
         * @returns {o}
         */
        tableIns: function(e, o, t, n, i, c) {
            h = o;
            m = t;
            if (!n || n == "") n = cUrl + "/index";
            var l = d("#param").val();
            if (l) {
                l = JSON.parse(l);
                if (d.isArray(l)) {
                    for (var a in l) {
                        console.log("索引：" + a + ",参数：" + l[a]);
                        if (n.indexOf("?") >= 0) {
                            n += "&" + l[a]
                        } else {
                            n += "?" + l[a]
                        }
                    }
                }
                console.log("网络请求地址：" + n)
            }
            f = r.render({
                elem: "#" + h,
                url: n,
                method: "post",
                cellMinWidth: 150,
                page: true,
                page: {
                    layout: ["refresh", "prev", "page", "next", "skip", "count", "limit"],
                    curr: 1,
                    groups: 10,
                    first: "首页",
                    last: "尾页"
                },
                height: "full-160",
                limit: 20,
                limits: [20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 1e3],
                even: true,
                cols: [e],
                loading: true,
                done: function(t, e, o) {
                    console.log("TABLE加载成功");
                    if (y) {
                        var n = d(".layui-table-body").find("table").find("tbody");
                        var i = n.children("tr");
                        i.on("dblclick", function() {
                            var e = n.find(".layui-table-hover").data("index");
                            var o = t.data[e];
                            console.log("TABLE行对象ID：" + o.id);
                            u.edit(g, o.id, b, v)
                        })
                    }
                }
            });
            r.on("tool(" + h + ")", function(t) {
                var e = t.data,
                    o = t.event;
                //设置获取主键ID
                var id = '';
                if(c){
                    id = e[c];
                }else{
                    id = e.id;
                }
                // console.log(e[c]);
                // console.log("修改数据"+e.brokerid);
                if (o === "del") {
                    console.log("删除操作");
                    u.drop(id, function(e, o) {
                        if (o) {
                            console.log("删除成功");
                            t.del()
                        } else {
                            console.log("删除失败")
                        }
                    })
                } else if (o === "edit") {
                    console.log("编辑操作");
                    u.edit(g, id, b, v)
                } else if (o === "detail") {
                    console.log("详情操作");
                    u.detail(g, id, b, v)
                }
                if (m) {
                    m(o, e)
                }
            });
            r.on("toolbar(" + h + ")", function(e) {
                var o = r.checkStatus(e.config.id);
                if (e.event == "add") {
                    s.msg("您点击了添加按钮")
                }
            });
            r.on("checkbox(" + h + ")", function(e) {
                console.log(e.checked);
                console.log(e.data);
                console.log(e.type)
            });
            r.on("row(" + h + ")", function(e) {
                e.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
                var o = e.data;
                console.log("单击对象ID：" + o.id)
            });
            if (i) {
                r.on("sort(" + h + ")", function(e) {
                    console.log(e.field);
                    console.log(e.type);
                    console.log(this);
                    r.reload(h, {
                        initSort: e,
                        where: {
                            field: e.field,
                            order: e.type
                        }
                    })
                })
            }
            return this
        },
        setDbclick: function(e) {
            y = e;
            return this
        },
        treeIns: function(e, o, t, n, i) {
            if (!i || i == "") i = cUrl + "/index";
            var l = function() {
                var o;
                d.ajaxSetup({
                    async: false
                });
                d.post(i, {}, function(e) {
                    if (e.success) {
                        o = e.data
                    } else {}
                }, "json");
                d.ajaxSetup({
                    async: true
                });
                return o
            };
            var a = layui.treeGird({
                elem: "#" + o,
                spreadable: t,
                checkbox: true,
                nodes: l(),
                layout: e
            });
            c.render();
            d("#collapse").on("click", function() {
                layui.collapse(a);
                return false
            });
            d("#expand").on("click", function() {
                layui.expand(a);
                return false
            });
            c.on("checkbox(*)", function(e) {
                console.log(e.elem);
                console.log(e.elem.checked);
                console.log(e.value);
                console.log(e.othis);
                var o = layui.getSelected(a);
                console.log(o.length)
            });
            d(".btnAdd2").click(function() {
                var e = d(this).attr("data-id");loadSlide
                if (n) {
                    n("btnAdd2", 0, e)
                } else {
                    u.edit(g, 0, b, v, ["pid=" + e])
                }
            });
            d(".btnEdit").click(function() {
                var e = d(this).attr("data-id");
                if (n) {
                    n("btnEdit", e, 0)
                } else {
                    u.edit(g, e, b, v)
                }
            });
            d(".btnDel").click(function() {
                var e = d(this).attr("data-id");
                if (n) {
                    n("btnDel", e, 0)
                } else {
                    u.drop(e, function(e, o) {
                        console.log("树节点已删除")
                    })
                }
            });
            d(".btnSetAuth").click(function() {
                var e = d(this).attr("data-id");
                if (n) {
                    n("btnSetAuth", e, 0)
                }
            })
        },
        setWin: function(e, o, t) {
            g = e;
            b = o;
            v = t;
            return this
        },
        searchForm: function(e, o) {
            c.on("submit(" + e + ")", function(e) {
                u.searchForm(r, e, o);
                return false
            })
        },
        getCheckData: function(e) {
            if (!e || e == "") e = h;
            var o = r.checkStatus(e),
                t = o.data;
            return t
        },
        initDate: function(e, t) {
            u.initDate(e, function(e, o) {
                if (t) {
                    t(e, o)
                }
            })
        },
        showWin: function(e, o, t, n, i, l, a, c) {
            u.showWin(e, o, t, n, i, l, a, function(e, o) {
                if (c) {
                    c(e, o)
                }
            })
        },
        ajaxPost: function(e, o, t, n) {
            u.ajaxPost(e, o, t, n)
        }
    };
    d(".btnBatchDrop").click(function() {
        var e = r.checkStatus(h),
            o = e.data;
        u.batchDrop(o, "submitForm", function() {
            console.log("批量删除成功");
            f.reload()
        })
    });
    u.verify();
    c.on("submit(searchForm)", function(e) {
        u.searchForm(r, e);
        return false
    });
    d(".btnAdd").click(function() {
        var e = d(this).attr("data-param");
        if (e) {
            e = JSON.parse(e)
        }
        console.log("自定义参数：" + e);
        u.edit(g, 0, b, v, e)
    });
    c.on("submit(submitForm)", function(e) {
        u.submitForm(e);
        return false
    });
    e("func", o)
});